package 双向无头链表;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-07-09
 * Time: 20:28
 */
public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;//表示当前链表的尾节点

    //打印
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //display的重载方法
    public void display(ListNode newHead){
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        //查看是否是第一次插入
        if(head == null){
            head = node;
            last = node;
        }else{
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = last.next;
        }
    }

    //获取到当前的index位置的(下标)相关节点，返回是ListNode类型
    public ListNode findIndex(int index){
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    //判断链表中是否只有一个头节点
                    if(head != null){
                        head.prev = null;//如果后续有节点，把当前head的prev给置null即可
                    }
                }else{
                    cur.prev.next = cur.next;
                    //不是尾节点
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        last =cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    //判断链表中是否只有一个头节点
                    if(head != null){
                        head.prev = null;//如果后续有节点，把当前head的prev给置null即可
                    }
                }else{
                    cur.prev.next = cur.next;
                    //不是尾节点
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        last =cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
    /**
     * 递归实现  单链表的逆序打印
     * @param head
     */
    public void printList(ListNode head){
        if(head == null){
            return;
        }
        if(head.next == null){
            System.out.print(head.val+" ");
            return;
        }
        printList(head.next);
        System.out.println(head.val+" ");
    }

    /**
     * 非递归实现
     * @param
     */
    public void printList2(ListNode head){
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while(cur != null){
            stack.push(cur);
            cur = cur.next;
        }
        while(!stack.empty()){
            ListNode top = stack.pop();
            System.out.print(top.val + " ");
        }

    }


}
